package com.ant.dao;

import java.util.List;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ant.form.User;
import com.ant.form.UserChatContent;

@Repository
public class UserDAOImpl implements UserDAO {

//    private static final Logger logger = LogManager.getLogger(UserDAOImpl.class);
    private static final String LIST_OTHER_USER_CHAT_CONTENT = "from UserChatContent userChatContent where userChatContent.user_id != ?";
//    private static final String GET_USER_BY_NAME = "from User user where user.name = ?";
    private static final String CHECK_USER_BY_NAME_PWD = "from User user where user.username =  ? and password = ?";
    private static final String CHECK_DUPLICATE_USER_BY_NAME = "from User user where user.username =  ?";
    @Autowired
    private SessionFactory sessionFactory;


    @Transactional
    public boolean checkUser(User user) {
        boolean rtValue = false;
        try{
            Query query = sessionFactory.getCurrentSession().createQuery(CHECK_USER_BY_NAME_PWD);
            query.setString(0, user.getUsername());
            query.setString(1, user.getPassword());
            List<User> users = query.list();
            if( null != users && users.size()> 0 ){
                rtValue = true;
            }
        }catch(Exception e){
        	  e.printStackTrace();
//            logger.warn("Select User Exception : ", e);
        }
        return rtValue;
    }

    @Transactional
    public boolean addUser(User user) {
        boolean rtValue = false;
        try{
            sessionFactory.getCurrentSession().save(user);
            rtValue = true;
        }catch(Exception e){
        	e.printStackTrace();
//            logger.warn("Save User Exception : ", e);
        }
        return rtValue;
    }
    
    @Transactional
    public boolean addUserChatContent(UserChatContent userContent) {
        boolean rtValue = false;
        try{
            sessionFactory.getCurrentSession().save(userContent);
            rtValue = true;
        }catch(Exception e){
        	e.printStackTrace();
//            logger.warn("Save User Exception : ", e);
        }
        return rtValue;
    }
    
    @Transactional
    public List<UserChatContent> listOtherUserChatContent(int user_id){
    	Session session = sessionFactory.getCurrentSession();
    	List<UserChatContent> result = session.createQuery(LIST_OTHER_USER_CHAT_CONTENT).list();
    	return result;
    }
    
    @Transactional
    public boolean checkDuplicateUser(User user){
    	 boolean rtValue = false;
         try{
             Query query = sessionFactory.getCurrentSession().createQuery(CHECK_DUPLICATE_USER_BY_NAME);
             query.setString(0, user.getUsername());
             List<User> users = query.list();
             if( null != users && users.size()> 0 ){
                 rtValue = true;
             }
         }catch(Exception e){
         	  e.printStackTrace();
//             logger.warn("Select User Exception : ", e);
         }
         return rtValue;
    }
//
//
//    @Transactional
//    public List<User> listUser() {
//        Session session = sessionFactory.getCurrentSession();
//        List<User> result = session.createQuery(LIST_USER_SQL).list();
//        return result;
//    }
//
//    @Transactional
//    public User getUser(long id) {
//        Session session = sessionFactory.getCurrentSession();
//        User user = (User)session.get(User.class, id);
//        return user;
//    }
//
//    @Transactional
//    public boolean removeUser(long id) {
//        User user = (User)sessionFactory.getCurrentSession().load(User.class, id);
//        sessionFactory.getCurrentSession().delete(user);
//        return true;
//    }
//
//    @Transactional
//    public boolean updateUser(User user) {
//        Session session = sessionFactory.getCurrentSession();
//        User userToUpdate =  (User)session.get(User.class, user.getId());
//        if(userToUpdate != null){
//            userToUpdate.setName(user.getName());
//            userToUpdate.setMember(user.getMember());
//            userToUpdate.setPassword(user.getPassword());
//            userToUpdate.setReset_password(user.getReset_password());
//            userToUpdate.setRole_id(user.getRole_id());
//            session.update(userToUpdate);
//            return true;
//        }
//
//        return false;
//
//    }
//
//    @Transactional
//    public User findUser(String userName) {
//        User user = null;
//        Session session = sessionFactory.getCurrentSession();
//        Query query =  session.createQuery( GET_USER_BY_NAME);
//        query.setString(0, userName);
//        List<User> result = query.list();
//        if(result!=null&&result.size()>0) {
//            user = result.get(0);
//        }
//        return user;
//    }

}
